package com.example.common.utils;



import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.Map;

public class JWTUtils {

    private static final String jwtToken = "123456Zhunk!@#$$";

    /*
    * 生成token
    * */
    public static String getToken(Map<String,String> map){
        Calendar instance = Calendar.getInstance();//默认7天过期
        instance.add(Calendar.DATE,7);
//        创建jwt builder
        JWTCreator.Builder builder = JWT.create();
        map.forEach((k,v)->{
            builder.withClaim(k,v);
        });
        String token = builder.withExpiresAt(instance.getTime())
                .sign(Algorithm.HMAC256(jwtToken));
        return token;
    }
    /*
    * 验证token的合法性
    * */
    public static DecodedJWT verify(String token){
        return JWT.require(Algorithm.HMAC256(jwtToken)).build().verify(token);
    }

    /*
    * 获取token信息方法
    * */
    public static DecodedJWT getTokenInfo(String token){
        DecodedJWT verify = JWT.require(Algorithm.HMAC256(jwtToken)).build().verify(token);
        return verify;
    }

}
